const Koa = require("koa");
const Router = require("koa-router");
const minify = require('html-minifier').minify;
const spider = require("./spider.js");
const requestLog = require("./request-log.js");

const app = new Koa();
const router = new Router();

// 记录请求次数
let reqTimes = 0
router.get(/.*/, async (ctx) => {
    reqTimes += 1
    // 记录请求日志
    requestLog(ctx.request, 'request.log', reqTimes)
    const url = "https://www.baidu.com" + ctx.originalUrl; // 目标网站URL
    try {
        let content = ''
        const { html = '获取html内容失败', contentType } = await spider(url)
        // 通过minify库压缩代码
        content = minify(html, { removeComments: true, collapseWhitespace: true, minifyJS: true, minifyCSS: true });
        if (ctx.request.url.indexOf(".") < 0) {
            ctx.set("Content-Type", "text/html");
        } else {
            ctx.set("Content-Type", contentType);
        }
        ctx.body = content; // 将HTML代码返回给前端
    } catch (error) {
        const errorObject = {
            message: error.message,
            name: error.name,
            stack: error.stack
        };
        // 记录错误日志
        requestLog(errorObject, 'error.log')
        ctx.body = '获取html内容失败';
    }
})

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000, () => {
    console.log("服务已启动在3000端口...");
});
